package as;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Product;


public class Main1Serv extends HttpServlet {
	private static final long serialVersionUID = 1L;

	List<Product> productList;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Main1Serv() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

       String select1 = request.getParameter("select1");
       String select2 = request.getParameter("select2");
       String select3 = request.getParameter("select3");
       String keyword = request.getParameter("keyword");



		Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/shangpin";
		String user = "root";
		String password = "rosy";

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, user, password);
			Statement stmt = conn.createStatement();
			String sql = "select * from product where" +"("+"ProductKbnA ='"+select1+"'"+ "AND" + " ProductKbnB ='"+select2+"'"+ "AND"+" ProductKbnC ='"+select3+"'"+")" +" OR"+ " ProductName like '"+"%"+keyword+"%"+"'";
			ResultSet rs = stmt.executeQuery(sql);

//			検索したデータを 配列 class にセット
			productList = new ArrayList<Product>();
//			List<Product>

            while(rs.next()) {
            	Product product = new Product();
            	product.setProductId(rs.getString("ProductId"));
            	product.setProductKbnA(rs.getString("ProductKbnA"));
            	product.setProductKbnB(rs.getString("ProductKbnB"));
            	product.setProductKbnC(rs.getString("ProductKbnC"));
            	product.setMaker(rs.getString("Maker"));
            	product.setProductName(rs.getString("ProductName"));

            	productList.add(product);
            }

            rs.close();
			stmt.close();
//			pstmt.close();
			conn.close();

//			throw new SQLException();

		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");
		} catch (SQLException e) {
			System.out.println("Sql exception");
		} catch (Exception e) {
			System.out.println("exception");
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				response.getWriter().println(e.getMessage());
			}
		}


//		response.sendRedirect("jsp/main2.jsp");




		request.setAttribute("products", productList);
		request.setAttribute("select1", select1);
		request.setAttribute("select2", select2);
		request.setAttribute("select3", select3);
		request.setAttribute("keyword", keyword);


		ServletContext context = this.getServletContext();
		RequestDispatcher dispatcher = context.getRequestDispatcher("/jsp/main2.jsp");
		dispatcher.forward(request, response);

	}

}
